package com.lagou.homework1_4.test04;

import com.lagou.net.User;
import com.lagou.net.UserMessage;

import java.io.*;
import java.net.Socket;

public class ClientTest {

    public static void main(String[] args) {

        Socket s = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;

        try {
            //创建Socket类型的对象并提供服务器的端口号和主机名
            s = new Socket("127.0.0.1", 8888);
            System.out.println("客户端连接成功");
            //使用输入输出流进行通信

            //0.使用对象流
            User user = new User("admin", "123456");
            UserMessage userMessage = new UserMessage("checked",user);
            oos = new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(userMessage);

            ois = new ObjectInputStream(s.getInputStream());
            Object object = ois.readObject();
            if (object instanceof com.lagou.net.UserMessage) {

                userMessage = (UserMessage)object;

                if ("success".equalsIgnoreCase(userMessage.getType())) {
                    System.out.println("登录成功");
                } else {
                    System.out.println("登录失败");
                }


            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {

                if (null != ois) {
                    ois.close();
                }

                if (null != oos) {
                    oos.close();
                }

                if (null != s) {
                    s.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
